home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_179 / unixutil / tee.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  70 lines

  1. /* tee.c - copy standard input to standard output and one other file.    *
  2.  *    This is useful for splitting (tapping) the pipe.  Presumes you    *
  3.  *    are using a shell program which can pipe the output from one    *
  4.  *    program into the input of another or some other pipe device,    *
  5.  *    but also can be used to make 2 copies of a file.        *
  6.  *                                    *
  7.  * tee <file1 >file2 file3                        *
  8.  * prog1 file1 | tee file3 | prog2 ...                    *
  9.  *                                    *
  10.  * tee (C) 1988 by Gary L. Brant                    *
  11.  *                                    *
  12.  * :ts=8                                */
  13.  
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #define MAXLINE 256
  17. #define ERROR    -1
  18.  
  19. void copy ();
  20. int close (), open (), read (), write ();
  21. int err, out2;            /* file handles */
  22.  
  23. main (argc, argv)
  24. int  argc;
  25. char *argv[];
  26. {
  27.  
  28.    err = fileno (stderr);
  29.    if (argc != 2) {
  30.       write (err, "usage: tee <file1 >file2 file3\n", 31);
  31.       exit (20);
  32.    }
  33.  
  34.    /* Manx documentation (lib.35) claims that if O_TRUNC is used, */
  35.    /* then O_CREAT is not needed; HOG_WASH; open() first deletes */
  36.    /* the file & then complains ENOENT (File does not exist!!! */
  37.  
  38.    if ((out2 = open (argv[1], O_WRONLY | O_TRUNC | O_CREAT)) == ERROR) {
  39.       write (err, "tee: cant open ", 15);
  40.       write (err, argv[1], strlen (argv[1]));
  41.       write (err, "\n", 1);
  42.       exit (20);
  43.    }
  44.  
  45.    copy (out2);
  46.    close (out2);
  47. }
  48.  
  49.  
  50. /* copy stdin to stdout and one other file */
  51.  
  52. void copy (out2)
  53. int out2;
  54. {
  55.    int p, in, out1;
  56.    char line[MAXLINE];
  57.  
  58.    in = fileno (stdin);
  59.    out1 = fileno (stdout);
  60.    while ((p = read (in, line, MAXLINE)) > 0) {
  61.       write (out1, line, p);
  62.       write (out2, line, p);
  63.    }
  64. }
  65.  
  66.  
  67. _wb_parse ()
  68. {
  69. }
  70.